﻿using MyEartags.EarTag;
using MyEartags.Enumerate;
using MyEartags.Individual;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace MyEartags.Zooid
{
    public class ZooidBuyAppService : ApplicationService, IZooidBuyAppService
    {
        private readonly IRepository<ZooidBuy, int> _zooidBuyRepository;

        private readonly IRepository<Earoverbit, int> _earoverbitRepository;

        public ZooidBuyAppService(IRepository<ZooidBuy, int> zooidBuyRepository, IRepository<Earoverbit, int> earoverbitRepository)
        {
            _zooidBuyRepository = zooidBuyRepository;
            _earoverbitRepository = earoverbitRepository;
        }

        /// <summary>
        /// 获取采购员信息
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<object> GetBuyerInfoList()
        {
            List<object> list = new List<object>();

            foreach (var item in Enum.GetValues<Buyer>())
            {
                list.Add(new
                {
                    buyerid = item,
                    buyername = item.ToString()
                });
            }

            return list;
        }

        /// <summary>
        /// 获取仓库信息
        /// </summary>
        /// <returns></returns>
        public List<object> GetWareHouseList()
        {
            List<object> list = new List<object>();

            foreach (var item in Enum.GetValues<WareHouse>())
            {
                list.Add(new
                {
                    warehouseid = item,
                    warehousename = item.ToString()
                });
            }

            return list;
        }

        /// <summary>
        /// 获取摘要信息
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<object> GetAbstractInfoList()
        {
            List<object> list = new List<object>();

            foreach (var item in Enum.GetValues<Abstract>())
            {
                list.Add(new
                {
                    abstractid = item,
                    abstractname = item.ToString()
                });
            }

            return list;
        }

        /// <summary>
        /// 羊只采购新增
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ZooidResult<ZooidBuyDto>> CreateSheepBuy(ZooidBuyDto dto)
        {
            ZooidBuy zooidBuy = new ZooidBuy();

            zooidBuy.Buyer = dto.Buyer;
            zooidBuy.Purchasedate = dto.Purchasedate;
            zooidBuy.Individualtype = dto.Individualtype;
            zooidBuy.Accountingunit = dto.Accountingunit;
            zooidBuy.Supplier = dto.Supplier;
            zooidBuy.Warehouse = dto.Warehouse;
            zooidBuy.Abstract = dto.Abstract;
            zooidBuy.Document = dto.Document;
            zooidBuy.overid = dto.overid;
            zooidBuy.Ridgepole = dto.Ridgepole;
            zooidBuy.Gield = dto.Gield;
            zooidBuy.Gender = dto.Gender;
            zooidBuy.Productname = dto.Productname;
            zooidBuy.Signbag = dto.Signbag;
            zooidBuy.Measuringunit = dto.Measuringunit;
            zooidBuy.Storagenum = dto.Storagenum;
            zooidBuy.Onlyprice = dto.Onlyprice;
            zooidBuy.Money = dto.Money;
            zooidBuy.Unitdiscount = dto.Unitdiscount;
            zooidBuy.Accountmoney = dto.Accountmoney;
            zooidBuy.TotalWeight = dto.TotalWeight;
            zooidBuy.Stage = dto.Stage;
            zooidBuy.Regulatenum = dto.Regulatenum;
            zooidBuy.Accountnum = dto.Accountnum;
            zooidBuy.Addpeople = dto.Addpeople;
            zooidBuy.AddDate = dto.AddDate = DateTime.Now;
            zooidBuy.Exitpeople = dto.Exitpeople;
            zooidBuy.Exitdate = dto.Exitdate = DateTime.Now; ;
            zooidBuy.Auditorpeople = dto.Auditorpeople;
            zooidBuy.Auditordate = dto.Auditordate = DateTime.Now; ;


            var result = await _zooidBuyRepository.InsertAsync(zooidBuy);

            var res = ObjectMapper.Map<ZooidBuy, ZooidBuyDto>(result);

            if (res != null)
            {
                return new ZooidResult<ZooidBuyDto>() { Code = 200, Message = "羊只采购成功！" };
            }
            else
            {
                return new ZooidResult<ZooidBuyDto>() { Code = 500, Message = "羊只采购失败！" };
            }
        }

        /// <summary>
        /// 获取个体采购列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<ZooidBuyShowDto>> GetZooidBuyShowDtoList()
        {
            var list = (from a in (await _zooidBuyRepository.GetListAsync())
                        join b in (await _earoverbitRepository.GetListAsync()) on a.overid equals b.Id
                        select new ZooidBuyShowDto
                        {
                            Id = a.Id,
                            Document = a.Document,
                            Purchasedate = a.Purchasedate,
                            Accountingunit = a.Accountingunit,
                            Individualtype = a.Individualtype,
                            Supplier = a.Supplier,
                            Abstract = a.Abstract,
                            OverbitName = b.OverbitName,
                            Ridgepole = a.Ridgepole,
                            Gield = a.Gield,
                            Gender = a.Gender,
                            Stage = a.Stage,
                            Buyer = a.Buyer,
                            Warehouse = a.Warehouse,
                            Productname = a.Productname,
                            Storagenum = a.Storagenum,
                            Regulatenum = a.Regulatenum,
                            Accountnum = a.Accountnum,
                            Onlyprice = a.Onlyprice,
                            Money = a.Money,
                            Unitdiscount = a.Unitdiscount,
                            Accountmoney = a.Accountmoney,
                            Addpeople = a.Addpeople,
                            AddDate = a.AddDate,
                            Exitpeople = a.Exitpeople,
                            Exitdate = a.Exitdate,
                            Auditorpeople = a.Auditorpeople,
                            Auditordate = a.Auditordate,
                            Signbag = a.Signbag,
                            Measuringunit = a.Measuringunit,
                            TotalWeight = a.TotalWeight
                        }).ToList();

            return list;
        }

        /// <summary>
        /// 删除个体采购
        /// </summary>
        /// <param name="bid"></param>
        /// <returns></returns>
        public async Task DeleteAsync(int bid)
        {
            await _zooidBuyRepository.DeleteAsync(bid);
        }

        /// <summary>
        /// 获取个体采购数据
        /// </summary>
        /// <param name="bid"></param>
        /// <returns></returns>
        public async Task<ZooidBuyDto> GetZooidData(int bid)
        {
            var list = await _zooidBuyRepository.GetAsync(bid);
            return ObjectMapper.Map<ZooidBuy, ZooidBuyDto>(list);
        }

        /// <summary>
        /// 更新个体采购数据
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="UserFriendlyException"></exception>
        public async Task<ZooidResult<ZooidBuyDto>> UpdateAsync(ZooidBuyDto dto)
        {
            var res = await _zooidBuyRepository.FindAsync(dto.Id);

            if (res == null)
            {
                throw new UserFriendlyException("没有找到对应用户！");
            }

            ObjectMapper.Map(dto, res);

            var count = await _zooidBuyRepository.UpdateAsync(res);
            var result = ObjectMapper.Map<ZooidBuy, ZooidBuyDto>(count);

            if(result != null)
            {
                return new ZooidResult<ZooidBuyDto>() { Code = 200, Message = "更新成功！" };
            }
            else
            {
                return new ZooidResult<ZooidBuyDto>() { Code = 500, Message = "更新失败！" };
            }
        }
    }
}
